#include "TcpConnect.hpp"
#include "InetAddress.hpp"
#include <netinet/in.h>
#include <sys/socket.h>

namespace wd{


TcpConnection::TcpConnection(int clientfd)
:_sock(clientfd)
,_sockio(clientfd)
,_localAddr(getLocalAddr())
,_peerAddr(getPeerAddr())
{
}

string TcpConnection::receive(){
    char buf[65535] = {0};
    int ret = _sockio.readline(buf,sizeof(buf));
    return string(buf,ret);
}

void TcpConnection::send(const string& msg){
    _sockio.sendn(msg.c_str(),msg.size());
}

struct sockaddr_in TcpConnection::getLocalAddr(){
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    socklen_t len = sizeof(addr);
    int ret = getsockname(_sock.fd(),(struct sockaddr*)&addr,&len);
    if(ret < 0){
        perror("getsockname");
    }
    return addr;
}
    
struct sockaddr_in TcpConnection::getPeerAddr(){
    struct sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    socklen_t len = sizeof(addr);
    int ret = getpeername(_sock.fd(),(struct sockaddr*)&addr,&len);
    if(ret < 0){
        perror("getpeername");
    }
    return addr;

}

}
